let lyricText;
let pHeight;
let lyricMainTop;
let lyricHeight;
let lyricMainDom = document.getElementById("lyMain");
let pTags;
let currentRow = 0;
let lastCurrentRow = -1;
let mainDom = document.getElementById("Main");
let audioDom = document.getElementById("myAudio");
let isArtificialSetTop = false;
let resizeTimer = setTimeout(function () {
    isArtificialSetTop = false
}, 2000);
let isGradual = true;
function main() {
    downloadLyricText();
    loadLyricMain();
    getPageData();
}
// 下载歌词
function downloadLyricText() {
    lyricText = lyricTextV2;
    lyricText = lry2json("[00:00.00]你[00:00.55]是[00:01.10]我[00:01.65]的[00:02.20]歌 [00:02.75](《[00:03.30]不[00:03.85]二[00:04.40]兄[00:04.95]弟[00:05.50]》[00:06.05]电[00:06.60]影[00:07.15]主[00:07.70]题[00:08.25]曲)[00:08.80] - [00:09.35]A-Lin[00:09.90]\n[00:09.90]词：[00:10.45]李[00:11.00]\n[00:12.10]曲：[00:12.65]影[00:13.20]\n[00:14.31]把[00:14.64]耳[00:15.06]朵[00:15.49]用[00:15.90]力[00:16.33]捂[00:16.77]住 [00:17.66]想[00:18.05]听[00:18.52]见[00:18.96]内[00:19.39]心[00:19.81]深[00:20.27]处[00:20.68]\n[00:21.99]那[00:22.44]微[00:22.84]弱[00:23.29]的[00:23.76]欢[00:24.46]呼[00:25.08]\n[00:27.95]心[00:28.35]跳[00:28.78]像[00:29.25]低[00:29.68]沉[00:30.15]的[00:30.54]鼓 [00:31.39]大[00:31.79]提[00:32.21]琴[00:32.64]拉[00:33.09]长[00:33.51]孤[00:33.95]独[00:34.20]\n[00:34.83]怎[00:35.29]么[00:35.70]也[00:36.18]唱[00:36.57]不[00:37.03]出[00:37.80]幸[00:38.26]福[00:38.93]\n[00:40.42]曾[00:40.82]经[00:41.30]在[00:41.47]悲[00:41.73]伤[00:42.14]旋[00:42.35]律[00:42.63]里[00:42.99]迷[00:43.18]了[00:43.47]路[00:43.71]\n[00:44.27]曾[00:44.46]经[00:44.72]在[00:44.95]纸[00:45.19]上[00:45.55]画[00:45.72]满[00:46.03]了[00:46.44]休[00:46.63]止[00:46.94]符[00:47.16]\n[00:48.12]舞[00:48.54]台[00:49.00]即[00:49.43]将[00:50.30]放[00:50.66]下[00:51.20]布[00:51.95]幕[00:52.97]\n[00:54.13]直[00:54.57]到[00:55.00]你[00:55.16]温[00:55.47]柔[00:55.81]坚[00:55.98]定[00:56.31]地[00:56.69]改[00:56.92]了[00:57.19]谱[00:57.42]\n[00:57.95]牵[00:58.14]起[00:58.43]我[00:58.61]的[00:58.88]手[00:59.24]轻[00:59.43]快[00:59.71]地[01:00.08]跳[01:00.31]着[01:00.62]舞[01:00.97]\n[01:01.83]蓦[01:02.24]然[01:02.67]回[01:03.17]首[01:03.59]才[01:03.93]发[01:04.22]觉[01:04.49]\n[01:04.86]原[01:05.06]来[01:05.39]你[01:05.75]一[01:06.15]直[01:06.53]都[01:07.02]是 [01:07.89]我[01:08.28]的[01:08.70]歌[01:09.70]\n[01:22.85]把[01:23.25]节[01:23.71]奏[01:24.12]放[01:24.56]慢[01:24.97]速[01:25.31]度 [01:26.22]让[01:26.64]我[01:27.05]能[01:27.50]跟[01:27.91]上[01:28.35]脚[01:28.76]步[01:29.16]\n[01:30.52]是[01:30.98]你[01:31.37]让[01:31.87]我[01:32.41]领[01:33.03]悟[01:33.94]\n[01:36.73]爱[01:37.02]比[01:37.38]想[01:37.79]象[01:38.22]中[01:38.69]顽[01:39.11]固 [01:39.93]在[01:40.39]副[01:40.77]歌[01:41.25]不[01:41.65]断[01:42.11]重[01:42.55]复[01:42.88]\n[01:43.39]预[01:43.81]告[01:44.22]着[01:44.66]我[01:45.06]们[01:45.52]的[01:46.02]演[01:46.81]出[01:47.78]\n[01:49.17]曾[01:49.51]经[01:49.86]在[01:50.03]悲[01:50.29]伤[01:50.62]旋[01:50.81]律[01:51.10]里[01:51.43]迷[01:51.70]了[01:51.96]路[01:52.16]\n[01:52.74]曾[01:52.93]经[01:53.20]在[01:53.43]纸[01:53.72]上[01:54.07]画[01:54.25]满[01:54.54]了[01:54.90]休[01:55.12]止[01:55.40]符[01:55.79]\n[01:56.70]舞[01:57.06]台[01:57.52]即[01:57.95]将[01:58.84]放[01:59.28]下[01:59.66]布[02:00.51]幕[02:01.09]\n[02:02.63]直[02:03.04]到[02:03.46]你[02:03.66]温[02:03.93]柔[02:04.29]坚[02:04.48]定[02:04.80]地[02:05.21]改[02:05.42]了[02:05.76]谱[02:05.94]\n[02:06.46]牵[02:06.65]起[02:06.89]我[02:07.11]的[02:07.43]手[02:07.77]轻[02:07.96]快[02:08.26]地[02:08.62]跳[02:08.81]着[02:09.12]舞[02:09.47]\n[02:10.46]蓦[02:10.82]然[02:11.20]回[02:11.62]首[02:12.01]才[02:12.43]发[02:12.81]觉[02:12.98]\n[02:13.34]原[02:13.56]来[02:13.87]你[02:14.29]一[02:14.65]直[02:15.00]都[02:15.49]是 [02:16.43]我[02:16.76]的[02:17.18]歌[02:17.41]\n[02:18.59]这[02:18.91]纷[02:19.33]乱[02:19.70]的[02:19.96]世[02:20.29]界 [02:21.83]充[02:22.06]满[02:22.26]美[02:22.52]丽[02:22.80]还[02:23.06]有[02:23.40]危[02:23.67]险[02:24.20]\n[02:24.54]谁[02:24.97]都[02:25.36]难[02:26.22]以[02:26.62]分[02:27.05]辨[02:28.16]\n[02:28.81]有[02:29.02]种[02:29.32]爱[02:29.63]被[02:29.93]叫[02:30.33]做[02:30.65]怨 [02:32.23]有[02:32.42]种[02:32.74]痛[02:33.08]却[02:33.38]叫[02:33.78]思[02:34.18]念[02:34.50]\n[02:35.65]总[02:35.83]让[02:36.12]人[02:36.30]分[02:36.49]不[02:36.73]出[02:37.07]界[02:37.43]线 [02:37.84]模[02:38.53]糊[02:39.03]了[02:39.87]感[02:41.06]觉[02:44.44]\n[02:47.30]曾[02:47.68]经[02:48.06]在[02:48.25]悲[02:48.54]伤[02:48.93]旋[02:49.09]律[02:49.40]里[02:49.77]迷[02:49.97]了[02:50.23]路[02:50.47]\n[02:51.10]曾[02:51.24]经[02:51.46]在[02:51.66]纸[02:51.97]上[02:52.31]画[02:52.53]满[02:52.84]了[02:53.20]休[02:53.62]止[02:53.93]符[02:54.20]\n[02:55.09]舞[02:55.47]台[02:55.84]即[02:56.23]将[02:57.05]放[02:57.51]下[02:57.99]布[02:58.74]幕[03:00.11]\n[03:01.03]直[03:01.41]到[03:01.73]你[03:01.90]温[03:02.23]柔[03:02.59]坚[03:02.77]定[03:03.07]地[03:03.47]改[03:03.69]了[03:04.00]谱[03:04.28]\n[03:04.77]牵[03:04.95]起[03:05.23]我[03:05.47]的[03:05.68]手[03:06.02]轻[03:06.19]快[03:06.48]地[03:06.86]跳[03:07.02]着[03:07.34]舞[03:07.82]\n[03:08.72]蓦[03:09.10]然[03:09.50]回[03:09.90]首[03:10.38]才[03:10.75]发[03:11.12]觉[03:11.26]\n[03:11.63]原[03:11.82]来[03:12.15]我[03:12.54]一[03:12.94]直[03:13.34]为[03:13.78]你 [03:14.76]唱[03:15.15]着[03:15.55]歌[03:17.67]\n")
}
function loadLyricMain() {
    lyricText.forEach(paragraphText => {
        let pElem = document.createElement("p");
        pElem.innerText = paragraphText.pText;
        lyricMainDom.appendChild(pElem);
    });
}
function getPageData() {
    pTags = lyricMainDom.getElementsByTagName("p");
    Array.from(pTags).forEach((pTag, index) => {
        pTag.addEventListener('click', function() {
            audioDom.currentTime = lyricText[index].startTime + 1
        });
    });
    pHeight = parseInt(window.getComputedStyle(pTags[0]).height);
    lyricHeight = parseInt(window.getComputedStyle(lyricMainDom).height);
    lyricMainTop = parseInt(window.getComputedStyle(mainDom).height) / 2;
    lyricMainDom.style.top = `${lyricMainTop}px`;
}
audioDom.addEventListener("timeupdate", async function () {
    currentRow = findNearest(lyricText, audioDom.currentTime);
    if (currentRow !== lastCurrentRow) {
        setLyMainTop();
        setLyPColor();
    }
    lastCurrentRow = currentRow;
});
function setLyMainTop() {
    if (!isArtificialSetTop) {
        let loadTop = parseInt(window.getComputedStyle(mainDom).height) / 2;
        let currentRowHeight = currentRow * -pHeight;
        lyricMainDom.style.top = `${(currentRowHeight + loadTop)}px`;
    }
}
function setLyPColor() {
    if (isGradual) {
        setGradualLyrics();
        return;
    }
    if (lastCurrentRow >= 0) {
        pTags[lastCurrentRow].style.color = "#fff";
    }
    pTags[currentRow].style.color = "#66ea88";
}
 function setGradualLyrics() {
    if (lastCurrentRow >= 0) {
        pTags[lastCurrentRow].innerHTML = "";
        pTags[lastCurrentRow].innerText = lyricText[lastCurrentRow].pText;
    }
    let pSpan = lyricText[currentRow].pSpan;
    pTags[currentRow].innerText = "";
    pSpan.forEach(p => {
        let spanElem = document.createElement("span");
        spanElem.innerText = p.text;
        pTags[currentRow].appendChild(spanElem);
    });
    gradual(pTags[currentRow], lyricText[currentRow]);

}
async function gradual(pTag, lyricT) {
    const lyricSpan = lyricT.pSpan;
    const spanTags = pTag.getElementsByTagName("span");
    for (let i = 0; i < spanTags.length; i++) {
        console.log(i);
        let j = 0;
        while (j <= 100 && j >= 0) {
            let number = (audioDom.currentTime - lyricSpan[i].startTime)*1000;
            j = parseInt((number/lyricSpan[i].playTime) * 100)
            try{spanTags[i].style.backgroundImage = `linear-gradient(to right, lightgreen ${j}%,#efedef 0%)`;}
            catch (e){
                return;
            }
            await sleep(10);
        }
    }
}

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
window.addEventListener("wheel", function (event) {
    isArtificialSetTop = true;
    let initTop = parseInt(window.getComputedStyle(lyricMainDom).top);
    clearTimeout(resizeTimer);
    // 判断鼠标滚轮滚动方向
    if (event.deltaY < 0) {
        // 向上滚动，减小top值
        initTop += pHeight * 3;
    } else {
        // 向下滚动，增加top值
        initTop -= pHeight * 3;
    }
    resizeTimer = setTimeout(function () {
        isArtificialSetTop = false;
        setLyMainTop()
    }, 1500);
    lyricMainDom.style.top = `${initTop}px`;
});


